import axios from "axios";
import {MyUrl} from "./Constant.ts";
import {hook_getFavorite} from "./UserHook.ts";
import {useToast} from "vue-toastification";

export function hook_checkSongIsFavorite(favoriteId, songId) {
    return axios.get(MyUrl.FAVORITE_checkSongIsFavorite, {
        params:{
            favoriteId: favoriteId,
            songId: songId,
        }
    }).then(res => {
        console.log(res.data)
        return res.data.flag
    }).catch(error => {
        console.error("检查是否是收藏歌曲失败：" + error)
    })
}

export function hook_checkPlaylistIsFavorite(playlistId:number) {
    return axios.get(MyUrl.FAVORITE_checkPlaylistIsFavorite, {
        params:{
            favoriteId: hook_getFavorite().id,
            playlistId: playlistId,
        }
    }).then(res => {
        console.log(res.data)
        return res.data.flag
    }).catch(error => {
        console.error("检查是否是收藏歌单失败：" + error)
    })
}


export function hook_findFavoriteSongByUserId(userId: number) {
    return axios.get(MyUrl.FAVORITE_findFavoriteSongByUserId, {
        params: {
            userId: userId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("获取歌曲失败：" + error)
    })
}
export function hook_findFavoritePlaylistByUserId(userId: number) {
    return axios.get(MyUrl.FAVORITE_findFavoritePlaylistByUserId, {
        params: {
            userId: userId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("获取收藏歌曲失败：" + error)
    })
}



//根据用户名Id和歌曲Id删除 FavoriteSong 中的纪录
export function hook_deleteFavoriteSongByUserId(userId: number, songId: number) {
    return axios.delete(MyUrl.FAVORITE_deleteFavoriteSongByUserId, {
        params: {
            userId: userId,
            songId: songId,
        }
    }).then(res => {
        console.log(res.data)
    }).catch(error => {
        console.error("删除收藏歌曲失败：" + error)
    })
}

//根据用户名Id和歌单Id删除 FavoritePlaylist 中的纪录
export function hook_deleteFavoritePlaylistByUserId(userId: number, playlistId: number) {
    return axios.delete(MyUrl.FAVORITE_deleteFavoritePlaylistByUserId, {
        params: {
            userId: userId,
            playlistId: playlistId,
        }
    }).then(res => {
        console.log(res.data)
    }).catch(error => {
        console.error("删除收藏歌单失败：" + error)
    })
}



//添加歌曲到Favorite
export function hook_addSongToFavorite(songId: number) {
    return axios.post(MyUrl.FAVORITE_addSongToFavorite, {
        favoriteId: hook_getFavorite()['id'],
        songId: songId,
    }).then(res => {
        console.log(res.data)
    }).catch(error => {
        console.error("收藏歌曲失败：" + error)
    })
}

//添加歌单到Favorite
export function hook_addPlaylistToFavorite(playlistId: number) {
    return axios.post(MyUrl.FAVORITE_addPlaylistToFavorite, {
        favoriteId: hook_getFavorite()['id'],
        playlistId: playlistId,
    }).then(res => {
        console.log(res.data)
        if(res.data.flag){
            useToast().success("收藏成功")
        }else{
            useToast().error("已被收藏")
        }
    }).catch(error => {
        console.error("收藏歌单失败：" + error)
        useToast().error("收藏歌单失败：" + error)
    })
}